/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BUS;

import DAO.MyMailDAO;
import DTO.MyMailDTO;
import doanlythuyet_javaoutlook.MyEmum.JEnum_TinhTrang;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 *
 * @author Dang Thi Phuong Thao
 */
public class MyMailBUS {

    private MyMailDAO m_MailDAO = new MyMailDAO();
    public ArrayList<MyMailDTO> layTatCaMail () throws IOException{
        return m_MailDAO.layTatCaMail();
    }
    public int themMail (MyMailDTO mailDTO) throws IOException{
        return m_MailDAO.themMail(mailDTO);
    }
    public int xoaMail(MyMailDTO mailDTO) throws IOException{
        return  m_MailDAO.xoaMail(mailDTO);
    }
    public ArrayList<MyMailDTO> layTatCaMailVoiDieuKien (String duongDan, Hashtable<String, String> cacDieuKien) throws IOException{
        return m_MailDAO.layTatCaMailVoiDieuKien(duongDan, cacDieuKien);
    }
    public int capNhatMail (MyMailDTO mailDTO) throws IOException{
        return m_MailDAO.capNhatMail(mailDTO);
    }
    public int thayDoiDuongDanThuMuc (String mailId, String thuMucDich) throws IOException{
        int soMailCapNhat = 0;
        Hashtable<String, String> cacDieuKien = new Hashtable<String, String>();
        cacDieuKien.put("Id", mailId);
        ArrayList<MyMailDTO> cacMailDTO = layTatCaMailVoiDieuKien(mailId, cacDieuKien);
        if (cacMailDTO.size() > 0){
            cacMailDTO.get(0).setM_DuongDanThuMuc(thuMucDich);
             soMailCapNhat = capNhatMail(cacMailDTO.get(0));
        }
        return soMailCapNhat;
    }
    public int xoaMail (String mailId) throws IOException{
        int soMailCapNhat = 0;
        MyMailDTO mailDTO = layMailVoidMailID(mailId);
        if (mailDTO != null){
            if (mailDTO.getM_DuongDanThuMuc().equals("Mails\\RecycleBins\\"))
                mailDTO.setM_TinhTrang(JEnum_TinhTrang.DaXoa.value());
            else
                mailDTO.setM_DuongDanThuMuc("Mails\\RecycleBins\\");
             soMailCapNhat = capNhatMail(mailDTO);
        }
        return soMailCapNhat;
    }

    public int thayDoiTinhTrangMail (String mailId, JEnum_TinhTrang tinhTrangMoi) throws IOException{
        int soMailCapNhat = 0;
        Hashtable<String, String> cacDieuKien = new Hashtable<String, String>();
        cacDieuKien.put("Id", mailId);
        ArrayList<MyMailDTO> cacMailDTO = layTatCaMailVoiDieuKien(mailId, cacDieuKien);
        if (cacMailDTO.size() > 0){
            cacMailDTO.get(0).setM_DuongDanThuMuc(String.valueOf(tinhTrangMoi.value()));
            soMailCapNhat = capNhatMail(cacMailDTO.get(0));
        }
        return soMailCapNhat;
    }

    public MyMailDTO layMailVoidMailID(String mailId) throws IOException {
        Hashtable<String, String> cacDieuKien = new Hashtable<String, String>();
        cacDieuKien.put("Id", mailId);
        ArrayList<MyMailDTO> cacMailDTO = layTatCaMailVoiDieuKien(mailId, cacDieuKien);
        if (cacMailDTO.size() > 0)
            return cacMailDTO.get(0);
        return null;
    }
}
